package web;

import com.alibaba.fastjson.JSON;
import pojo.Brand;
import pojo.pageBean;
import service.brandService.brandService;
import service.brandServiceImpl.brandServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/selectByPageServlet")
public class selectByPageServlet extends HttpServlet {
    brandService brandservice=new brandServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从前端获取到当前页面，和页面显示条数
        String _currentPage = req.getParameter("currentPage");
        String _pageSize = req.getParameter("pageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);
        List<Brand> rows = brandservice.selectByPage(currentPage, pageSize);
        int total = brandservice.selectTotal();
        //封装成一个pageBean对象
        pageBean pagebean=new pageBean();
        pagebean.setRows(rows);
        pagebean.setTotalCount(total);
        //转化为json对象
        String jsonString = JSON.toJSONString(pagebean);
        resp.setContentType("text/json;charset=utf-8");//设置防止中文乱码
        resp.getWriter().write(jsonString);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
